package com.stone.persistent.recyclerview.helper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;


import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;
import com.stone.persistent.recyclerview.R;
import com.stone.persistent.recyclerview.extensions.ActivityExtensions;
import com.stone.persistent.recyclerview.extensions.ContextExtensions;
import com.stone.persistent.recyclerview.library.ParentRecyclerView;



public class SyncScrollHelpers {

    public int statusBarHeight, screenHeight;
    private float toolbarHeight, searchBarHeight;
    private Context activity;
    private ConstraintLayout toolBarLayout, searchBarLayout;

    private ImageView backIv1, backIv2, logoImageView;
    private float floatVisibleThreshold;
    private FrameLayout floatAdLayout;
    private boolean floatAdClosed;
    private static final float BACK_DIMENSION_RATIO1 = 1.8125F;
    private static final float BACK_DIMENSION_RATIO2 = 0.992647F;

    public void initLayout() {
        ConstraintLayout constraintLayout = this.toolBarLayout;
        ViewGroup.LayoutParams layoutParams = constraintLayout.getLayoutParams();
        if (layoutParams == null) {
            try {
                throw new Exception("null cannot be cast to non-null type androidx.constraintlayout.widget.ConstraintLayout.LayoutParams");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ConstraintLayout.LayoutParams toolbarParams = (ConstraintLayout.LayoutParams) layoutParams;
            toolbarParams.setMargins(0, this.statusBarHeight, 0, 0);
            constraintLayout = this.toolBarLayout;
            constraintLayout.setLayoutParams((ViewGroup.LayoutParams) toolbarParams);
            float backImgHeight1 = (float) this.screenHeight / BACK_DIMENSION_RATIO1;
            float translationY1 = backImgHeight1 - (float) this.statusBarHeight - this.toolbarHeight - this.searchBarHeight;
            ImageView imageView = this.backIv1;
            imageView.setTranslationY(-translationY1);
            float backImgHeight2 = (float) this.screenHeight / BACK_DIMENSION_RATIO2;
            float translationY2 = backImgHeight2 - backImgHeight1;
            imageView = this.backIv2;
            imageView.setTranslationY(-translationY2);
            constraintLayout = this.searchBarLayout;
            constraintLayout.setTranslationY((float) this.statusBarHeight + this.toolbarHeight);
        }
    }

    public void syncRecyclerViewScroll( ParentRecyclerView recyclerView) {

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            public void onScrolled( RecyclerView recyclerView, int dx, int dy) {
                int scrollY = recyclerView.computeVerticalScrollOffset();
                float minTranslationY = (float) SyncScrollHelpers.this.statusBarHeight + ContextExtensions.dp2px((Context) SyncScrollHelpers.this.activity, 9.0F);
                float maxTranslationY = (float) SyncScrollHelpers.this.statusBarHeight + SyncScrollHelpers.this.toolbarHeight;
                float targetTranslationY = maxTranslationY - (float) scrollY * 0.7F;
                float alpha = (float) 1 - (float) (scrollY / 2) / (maxTranslationY - minTranslationY);
                if (alpha < (float) 0) {
                    alpha = 0.0F;
                }

                ImageView imageView = SyncScrollHelpers.this.logoImageView;

                imageView.setAlpha(alpha);
                ConstraintLayout constraintLayout = SyncScrollHelpers.this.searchBarLayout;

                constraintLayout.setTranslationY(targetTranslationY < minTranslationY ? minTranslationY : targetTranslationY);
                float maxMarginRight = ContextExtensions.dp2px((Context) SyncScrollHelpers.this.activity, 92.0F);
                float progress = ((float) 1 - alpha) * 2.0F;
                if (progress > (float) 1) {
                    progress = 1.0F;
                }

                constraintLayout = SyncScrollHelpers.this.searchBarLayout;

                ViewGroup.LayoutParams var13 = constraintLayout.getLayoutParams();
                if (var13 == null) {
                    try {
                        throw new Exception("null cannot be cast to non-null type androidx.constraintlayout.widget.ConstraintLayout.LayoutParams");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) var13;
                    layoutParams.setMargins(0, 0, (int) (maxMarginRight * progress), 0);
                    constraintLayout = SyncScrollHelpers.this.searchBarLayout;

                    constraintLayout.setLayoutParams((ViewGroup.LayoutParams) layoutParams);
                    if (!SyncScrollHelpers.this.floatAdClosed) {
                        FrameLayout var14 = SyncScrollHelpers.this.floatAdLayout;
                        var14.setVisibility((float) scrollY > SyncScrollHelpers.this.floatVisibleThreshold ? View.VISIBLE : View.GONE);
                    }

                }
            }
        });
    }

    public void syncRefreshPullDown( SmartRefreshLayout refreshLayout) {

        SimpleMultiPurposeListener purposeListener = new SimpleMultiPurposeListener() {
            public void onHeaderMoving( RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
                float backImgHeight1 = (float) SyncScrollHelpers.this.screenHeight / BACK_DIMENSION_RATIO1;
                float backImgHeight2 = (float) SyncScrollHelpers.this.screenHeight / BACK_DIMENSION_RATIO2;
                float maxTranslationY = backImgHeight1 - (float) SyncScrollHelpers.this.statusBarHeight - SyncScrollHelpers.this.toolbarHeight - SyncScrollHelpers.this.searchBarHeight;
                ImageView imageView;
                float outOfOffset;
                float translationY2;
                ConstraintLayout var13;
                if ((float) offset > maxTranslationY) {
                    outOfOffset = (float) offset - maxTranslationY;
                    imageView = SyncScrollHelpers.this.backIv1;

                    imageView.setAlpha(0.0F);
                    var13 = SyncScrollHelpers.this.toolBarLayout;

                    var13.setAlpha(0.0F);
                    var13 = SyncScrollHelpers.this.searchBarLayout;

                    var13.setAlpha(0.0F);
                    imageView = SyncScrollHelpers.this.backIv1;

                    imageView.setTranslationY(0.0F);
                    translationY2 = backImgHeight2 - backImgHeight1 - outOfOffset;
                    imageView = SyncScrollHelpers.this.backIv2;

                    imageView.setTranslationY(-translationY2);
                } else {
                    outOfOffset = (maxTranslationY - (float) offset) / maxTranslationY;
                    imageView = SyncScrollHelpers.this.backIv1;

                    imageView.setAlpha(outOfOffset);
                    var13 = SyncScrollHelpers.this.toolBarLayout;

                    var13.setAlpha(outOfOffset);
                    var13 = SyncScrollHelpers.this.searchBarLayout;

                    var13.setAlpha(outOfOffset);
                    translationY2 = maxTranslationY - (float) offset;
                    imageView = SyncScrollHelpers.this.backIv1;

                    imageView.setTranslationY(-translationY2);
                    float translationY2x = backImgHeight2 - backImgHeight1;
                    imageView = SyncScrollHelpers.this.backIv2;

                    imageView.setTranslationY(-translationY2x);
                }

            }
        };
        refreshLayout.setOnMultiPurposeListener(purposeListener);
    }

    public SyncScrollHelpers(Context mainActivity) {
        super();


        this.statusBarHeight = ActivityExtensions.getStatusBarHeight((FragmentActivity) mainActivity);
        this.toolbarHeight = ContextExtensions.dp2px((Context) mainActivity, 50.0F);
        this.screenHeight = ActivityExtensions.getScreenWidth((FragmentActivity) mainActivity);
        this.searchBarHeight = ContextExtensions.dp2px((Context) mainActivity, 46.0F);
        this.activity = mainActivity;


        this.toolBarLayout = (ConstraintLayout) ((FragmentActivity) mainActivity).findViewById(R.id.main_toolbar);
        this.searchBarLayout = (ConstraintLayout) ((FragmentActivity) mainActivity).findViewById(R.id.main_search_layout);
        this.backIv1 = (ImageView) ((FragmentActivity) mainActivity).findViewById(R.id.main_back_img1);
        this.backIv2 = (ImageView) ((FragmentActivity) mainActivity).findViewById(R.id.main_back_img2);
        this.logoImageView = (ImageView) ((FragmentActivity) mainActivity).findViewById(R.id.main_top_logo);
        this.floatVisibleThreshold = ContextExtensions.dp2px((Context) mainActivity, 500.0F);
        this.floatAdLayout = (FrameLayout) ((FragmentActivity) mainActivity).findViewById(R.id.home_float_layout);
        final ParentRecyclerView recyclerView = (ParentRecyclerView) ((FragmentActivity) mainActivity).findViewById(R.id.main_recycler_view);
        int stickyHeight = (int) ContextExtensions.dp2px((Context) mainActivity, 50.0F);
        recyclerView.setStickyHeight(stickyHeight);


        ((ImageView) ((FragmentActivity) mainActivity).findViewById(R.id.home_float_close_btn)).setOnClickListener((new View.OnClickListener() {
            public final void onClick(View it) {
                SyncScrollHelpers.this.floatAdClosed = true;
                FrameLayout frameLayout = SyncScrollHelpers.this.floatAdLayout;
                frameLayout.setVisibility(View.GONE);
                recyclerView.setStickyHeight(0);
            }
        }));
    }


}
